<?php
	defined( "AZ_EXE_" ) or die ( "CORE VARIABLE IS NOT DEFINED." );
	class MenuController extends Controller {
		public function index(){
			$start = Basic::getVar("start");
			$perpage = Basic::getVar("perpage");
			$search = Basic::getVar("search");
			$start = is_numeric($start)&&$start>-1?$start:0;
			$perpage = is_numeric($perpage)&&$perpage>-1?$perpage:20;
			$searchColumns = Basic::getVar("search_column")?Basic::getVar("search_column"):false;
			$sortColumn = Basic::getVar("sort_column")?Basic::getVar("sort_column"):"menutype,ordering,name_en";
			$sortCriteria = Basic::getVar("sort_criteria")?Basic::getVar("sort_criteria"):false;
			$menu = $this->loadModel("Menu");
			$menuRecords = $menu->display($start,$perpage,$search,$searchColumns,$sortColumn,$sortCriteria) ;
			$totalRecords = $menu->getTotalRecords();
			if ( $start < $totalRecords ){
				$this->set("totalRows",$totalRecords);
			}elseif ( $start >= $totalRecords ) {
				$start = $start-20;
				$menuRecords = $menu->display($start,$perpage,$search,$searchColumns,$sortColumn,$sortCriteria) ;
				$this->set("totalRows",$totalRecords);
			}
			$this->set("currentPage",$start);
			$this->set("perpage",$perpage);
			$this->set("search",$search);
			$this->set("searchColumns",$searchColumns);
			$this->set("sortColumn",$sortColumn);
			$this->set("sortCriteria",$sortCriteria);
			$this->set("search",$search);
			$this->set("menus",$menuRecords);
			$this->set("title","SMECA CLINIC - Menu List");
			$this->set("selectedMenu","MEN");
			$this->renderDefault("display","menu");
		}	
		public function add(){
			$menutype = $this->loadModel("Menu");
			//$route = $this->loadModel("Route");
			$article = $this->loadModel("Article");
			$this->set( "menutypes" , $menutype->getMenuType() );
			$this->set("articles",$article->getAllArticles());
			$this->set("title","SMECA CLINIC - Add Menu");
			$this->set("selectedMenu","MEN");
			$this->renderDefault("add","menu" );
		}
		public function edit(){
			$id = Basic::getVar("id");
			$menu = $this->loadModel("Menu");
			$article = $this->loadModel("Article");
			if ( $id ){
				$menuinfo = $menu->find( $id );
				$this->set( "menutypes" , $menu->getMenuType() );
				$this->set("articles",$article->getAllArticles());
				$this->set("menu",$menuinfo[0]);
				$this->set("title","SMECA CLINIC - Edit Menu");
				$this->set("selectedMenu","MEN");
				$this->renderDefault("edit", "menu" );
			}else{
				//$this->index();
				$this->redirect( array("com"=>"menu","action"=>"index"));
			}
		}
		public function editprocess(){
			$menu = $this->loadModel("Menu");
			if(!empty($_POST)){
				$this->set("info","Controller information is updated succeffully.");
				$menu->update($_POST['data']);
			}else{
				$this->set("info","Failed to update menu information.");
			}
			//$this->index();
			$this->redirect( array("com"=>"menu","action"=>"index"));
		}
		public function addprocess(){
			$menu = $this->loadModel("Menu");
			$menu->add($_POST['data']);
			//$this->index();
			$this->redirect( array("com"=>"menu","action"=>"index"));
		}
		public function delete(){
			$ids = Basic::getVar("id");
			$menu = $this->loadModel("Menu");
			if ( is_array($ids) || !empty($ids) ){
				$this->set("info","Menu(s) delete successfully.");
				$menu->delete( $ids );
			}else{
				$this->set("info","Failed to delete menu(s) successfully.");
			}
			//$this->index();
			$this->redirect( array("com"=>"menu","action"=>"index"));
		}
		public function category2($mid = 0){
			echo "<h1 style='width:50%;margin:35px auto;text-align:center;border:1px solid #eee;padding:20px'>Page Under Construction</h1>";
			header("refresh:3;url=./");
		}
		public function category(){
			$id = Basic::getVar("mid");
			$alias = Basic::getVar("alias");
			Import::AppLib("word");
			
			$track = $this->loadModel("TrackVisitor");
			$track -> trackVisitor();
			$visitors = $track->getVisitor();
			$this->set('visitors',$visitors);
		
			$banner = $this->loadModel( "Banner" );
			$this->set("banners",$banner->display());
			$menu = $this->loadModel("Menu");
			$menus = $menu->getMenu("Section Menues","en");
			$footer_menus = $menu->getMenu("Section Footer","en");
			$this->set('menus',$menus);
			$this->set('footerMenues',$footer_menus);
			$this->set("topBanners",$banner->topBanners());
			$this->set("rightBanners",$banner->rightBanners());
			$this->set("bottomBanners",$banner->bottomBanners());
			
			if($alias == "start-up-guides"){
				$art = $this->loadModel("Article");
				$top_artilces = $art->getTopArticleByMenuID($id,4);
				$latest_articles = $art->getLatestArticleByMenuID($id,5);//Basic::pr($top_artilces);die();
				$this->set('top_articles',$top_artilces);
				$this->set('latest_articles',$latest_articles);
			}else{
				list($categories,$menu_name) = $menu->getCategory($id);
				$this->set('categories',$categories);//die(Basic::pr($categories));
			}
			$this->set('menu_id',$id);
			$this->set( "title" , "SMECA CLINIC - " . $menu_name );
			$this->renderLayout("frontListCategory");
		}
        public function categoryarticle(){
            $id = Basic::getVar("mid");
            $cid = Basic::getVar("cid");
            $page = Basic::getVar("page");
            $page = $page<1?1:$page;
            $this->set("page",$page);
            $this->set('menu_id',$id);
            $this->set("cat_id",$cid);
            Import::AppLib("word");
            $menu = $this->loadModel("Menu");
            list($categories,$menu_name) = $menu->getCategory($id);
            
            $this->set('categories',$categories);//die(Basic::pr($categories));
            return $this->renderDefault("articlecategory","menu");
        }
	}